package com.thirteenClassics.async;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CachePut;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.thirteenClassics.cache.JdbSentenceCache;
import com.thirteenClassics.dto.CommonDTO;
import com.thirteenClassics.dto.MarkCatalogQueryDTO;
import com.thirteenClassics.dto.PictureCatalogQueryDTO;
import com.thirteenClassics.dto.SentenceSelectDYBDTO;
import com.thirteenClassics.entity.Book;
import com.thirteenClassics.entity.Catalog;
import com.thirteenClassics.entity.CatalogBook;
import com.thirteenClassics.entity.Entity;
import com.thirteenClassics.entity.Explain;
import com.thirteenClassics.entity.Sentence;
import com.thirteenClassics.exception.BaseException;
import com.thirteenClassics.vo.MarkCatalogQueryJdbVO;
import com.thirteenClassics.vo.SentenceVO;
import com.thirteenClassicsServer.mapper.BookMapper;
import com.thirteenClassicsServer.mapper.CatalogBookMapper;
import com.thirteenClassicsServer.mapper.DictionaryMapper;
import com.thirteenClassicsServer.mapper.EntityMapper;
import com.thirteenClassicsServer.mapper.EntryMapper;
import com.thirteenClassicsServer.mapper.ExplainMapper;
import com.thirteenClassicsServer.mapper.MarkMapper;
import com.thirteenClassicsServer.mapper.OperationHistoryMapper;
import com.thirteenClassicsServer.mapper.PictureMapper;
import com.thirteenClassicsServer.mapper.SemanticMapper;
import com.thirteenClassicsServer.mapper.SentenceLinkMapper;
import com.thirteenClassicsServer.mapper.SentenceMapper;
import com.thirteenClassicsServer.mapper.UserMapper;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class MarkServiceAsync {
    @Resource
    private UserMapper userMapper;
    @Resource
    private MarkMapper markMapper;
    @Resource
    private EntryMapper entryMapper;
    @Resource
    private SentenceMapper sentenceMapper;
    @Resource
    private SemanticMapper semanticMapper;
    @Resource
    private DictionaryMapper dictionaryMapper;
    @Resource
    private BookMapper bookMapper;
    @Resource
    private PictureMapper pictureMapper;
    @Resource
    private CatalogBookMapper catalogBookMapper;

    @Resource
    private EntityMapper entityMapper;

    @Resource
    private ExplainMapper explainMapper;

    @Resource
    private SentenceLinkMapper sentenceLinkMapper;

    @Resource
    private OperationHistoryMapper operationHistoryMapper;

    @Resource
    private JdbSentenceCache jdbSentenceCache;


    @Async
    @CachePut(value = "JdbMarkCache", key = "'JdbMark-' + #markCatalogQueryDTO.bookId + '-' + #markCatalogQueryDTO"
            + ".bookName + '-' + #markCatalogQueryDTO.index + '-' + #markCatalogQueryDTO.sentenceIndex + '-' + "
            + "#markCatalogQueryDTO.paragraphIndex")
    public MarkCatalogQueryJdbVO selectJdbCatalogAndAllFlashCache(MarkCatalogQueryDTO markCatalogQueryDTO) {
        MarkCatalogQueryJdbVO markCatalogQueryJdbVO = new MarkCatalogQueryJdbVO();
        Book book = bookMapper.selectbookById(markCatalogQueryDTO.getBookId());
        if (book == null) {
            throw new BaseException("不存在书目");
        }

        PictureCatalogQueryDTO pictureCatalogQueryDTO = new PictureCatalogQueryDTO();
        BeanUtils.copyProperties(markCatalogQueryDTO, pictureCatalogQueryDTO);

        //得到所有的目录信息
        List<Catalog> list = pictureMapper.selectCatalogOrdersByName(pictureCatalogQueryDTO);

        markCatalogQueryJdbVO.setId(pictureCatalogQueryDTO.getIndex());
        markCatalogQueryJdbVO.setName(list.get(pictureCatalogQueryDTO.getIndex().intValue() - 1).getContents());
        markCatalogQueryJdbVO.setAncestors(list.get(pictureCatalogQueryDTO.getIndex().intValue() - 1).getAncestors());
        markCatalogQueryJdbVO.setTot(Long.valueOf(list.size()));
        markCatalogQueryJdbVO.setCatalogId(list.get(pictureCatalogQueryDTO.getIndex().intValue() - 1).getId());

        CommonDTO commonDTO = new CommonDTO();
        commonDTO.setCatalogId(list.get(pictureCatalogQueryDTO.getIndex().intValue() - 1).getId());
        commonDTO.setBookId(markCatalogQueryDTO.getBookId());
        //根据书id和catalogid查到在catalog_book中的记录
        CatalogBook catalogBook = catalogBookMapper.selectCatalogBookAll(commonDTO);
        if (catalogBook == null) {
            throw new BaseException("书内容不存在");
        }


        markCatalogQueryJdbVO.setEntryId(catalogBook.getId());

        //经典版的所有句子
        Sentence sentence = new Sentence();
        sentence.setEntryId(catalogBook.getId());

        List<SentenceVO> sentenceVOS = jdbSentenceCache.queryJdbSentence(sentence);
        markCatalogQueryJdbVO.setJdbContent(sentenceVOS);

        //得到sentenceId
        SentenceSelectDYBDTO sentenceSelectDYBDTO3 = new SentenceSelectDYBDTO();
        sentenceSelectDYBDTO3.setEntryId(catalogBook.getId());
        sentenceSelectDYBDTO3.setSentenceIds(markCatalogQueryDTO.getSentenceIndex());
        sentenceSelectDYBDTO3.setParagraphIds(markCatalogQueryDTO.getParagraphIndex());
        List<Sentence> sentence3 = sentenceMapper.selectSentenceJdbId(sentenceSelectDYBDTO3);
        if (sentence3.isEmpty()) {
            return markCatalogQueryJdbVO;
        }
        Long sentenceId = sentence3.get(0).getSentenceId();

        SentenceSelectDYBDTO sentenceSelectDYBDTO2 = new SentenceSelectDYBDTO();

        //经典版的当前句子
        sentenceSelectDYBDTO2.setEntryId(catalogBook.getId());
        sentenceSelectDYBDTO2.setSentenceIds(markCatalogQueryDTO.getSentenceIndex());
        sentenceSelectDYBDTO2.setParagraphIds(markCatalogQueryDTO.getParagraphIndex());
        Sentence sentence1 = sentenceMapper.selectByEntryIDAndOrderJdb(sentenceSelectDYBDTO2);

        markCatalogQueryJdbVO.setSentence(sentence1.getSentence());
        markCatalogQueryJdbVO.setExplainS(sentence1.getExplainS());
        markCatalogQueryJdbVO.setFc(sentence1.getFc());


        List<Entity> entities = entityMapper.selectBySentenceId(sentenceId, Long.valueOf(1), markCatalogQueryDTO.getBookName());


        markCatalogQueryJdbVO.setEntities(entities);

        List<Explain> explanations = explainMapper.selectBySentenceId(sentenceId, Long.valueOf(1), markCatalogQueryDTO.getBookName());


        markCatalogQueryJdbVO.setExplanations(explanations);
        return markCatalogQueryJdbVO;
    }


}
